package com.party.service.partylog.impl;

import com.party.common.Page;
import com.party.mapper.PartyLogMapper;
import com.party.model.PartyLog;
import com.party.model.PartyLogExample;
import com.party.service.partylog.PartyLogService;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: beyond
 * Date: 14-12-11
 * Time: 下午2:41
 * To change this template use File | Settings | File Templates.
 */
@Repository
public class PartyLogServiceImpl implements PartyLogService{

    @Resource
    PartyLogMapper partyLogMapper;

    @Override
    public int addPartyLog(PartyLog partyLog) {
        int num = partyLogMapper.insertSelective(partyLog);
        return num;
    }

    @Override
    public int deletePartyLogByPrimaryKey(Integer id) {
        int num = partyLogMapper.deleteByPrimaryKey(id);
        return num;
    }

    @Override
    public int deleteExamByIds(String ids) {
        int num = 0;
        if (ids != null&&!"".equals(ids)) {
            String roids[] = ids.split(",");
            for (String roid : roids) {
                num += partyLogMapper.deleteByPrimaryKey(Integer.parseInt(roid));
            }
        }
        return num;
    }

    @Override
    public PartyLog getPartyLogByPrimaryKey(Integer id) {
        PartyLog partyLog = partyLogMapper.selectByPrimaryKey(id);
        return partyLog;
    }

    @Override
    public int updatePartyLogByPrimaryKey(PartyLog partyLog) {
        int num = partyLogMapper.updateByPrimaryKeySelective(partyLog);
        return num;
    }

    @Override
    public Page getPartyLogList(PartyLog partyLog, int pageSize, int pageNumber) {
        PartyLogExample partyLogExample = new PartyLogExample();
        /**
         * 可以添加查询条件
         */
        if(partyLog != null)
        {
            PartyLogExample.Criteria criteria = partyLogExample.createCriteria();
            if(partyLog.getAuthor() != null && !"".equals(partyLog.getAuthor()))
            {
                criteria.andAuthorLike("%"+partyLog.getAuthor()+"%");
            }

            if(partyLog.getTime() != null && !"".equals(partyLog.getTime()))
            {
                criteria.andTimeGreaterThanOrEqualTo(partyLog.getTime());
            }
        }
        partyLogExample.setOrderByClause("time desc");
        partyLogExample.setOrderByClause("partyStage desc");

        //获取总的记录数量
        int count = partyLogMapper.countByExample(partyLogExample);
        //获取指定的页码数据
        partyLogExample.setBeginNum((pageNumber - 1) * pageSize);
        partyLogExample.setEndNum(pageSize);
        List<PartyLog> list = partyLogMapper.getListPage(partyLogExample);
        //组合返回page
        Page page = new Page(pageNumber, count, pageSize, list);

        return page;
    }
}
